package com.hue.springbootredis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ResponseBody
    @RequestMapping("/redis")
    public String redis() {
        String result = redisTemplate.opsForValue().get("key");
        if (result == null) {
            System.out.println("从数据库获取...");
            redisTemplate.opsForValue().set("key", "hello");
            return "hello";
        }
        System.out.println("从redis中获取");
        return result;
    }
}
